package cn.tedu.boyou.acticle.service.impl;

import cn.tedu.boyou.acticle.domain.*;
import cn.tedu.boyou.acticle.mapper.*;
import cn.tedu.boyou.acticle.service.IStrategyDetailService;
import cn.tedu.boyou.acticle.vo.StrategyStatisVO;
import cn.tedu.boyou.common.query.StrategyDetailQuery;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional
public class StrategyDetailServiceImpl implements IStrategyDetailService {
    @Autowired
    private StrategyDetailMapper strategyDetailMapper;
    @Autowired
    private StrategyTagMapper strategyTagMapper;
    @Autowired
    private StrategyMapper strategyMapper;
    @Autowired
    private DestinationMapper destinationMapper;
    @Autowired
    private StrategyContentMapper strategyContentMapper;

    public PageInfo query(StrategyDetailQuery qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<StrategyDetail> details = strategyDetailMapper.selectForList(qo);
        return new PageInfo<>(details);
    }

    public void saveOrUpdate(StrategyDetail strategyDetail, String[] tags) {

        //前台没有传过dest所以根据大攻略查询
        Strategy strategy = strategyMapper.selectByPrimaryKey(strategyDetail.getStrategy().getId());
        //所属地区
        Destination dest = strategy.getDest();
        strategyDetail.setDest(dest);
        //文章的长度
        int length = strategyDetail.getStrategyContent().getContent().length();
        //文章内容
        String content = strategyDetail.getStrategyContent().getContent();
        //摘取文章内容的前200字
        if(strategyDetail.getStrategyContent() != null && length > 0 && length < 200){
            strategyDetail.setSummary(content);
        }else if(strategyDetail.getStrategyContent() != null  && length > 200){
            strategyDetail.setSummary(content.substring(0,200));
        }else{
            strategyDetail.setSummary(null);
        }



        //判断是否为国外
        boolean china = isChina(dest);
        strategyDetail.setIsabroad(!china);

        if(strategyDetail.getId() != null){
            //修改
            //维护第三张表的关系
            strategyDetailMapper.deleteRelation(strategyDetail.getId());
            //创建标签对象
            StrategyTag tag = new StrategyTag();
            //遍历添加标签
            for (String t : tags) {
                tag.setName(t);
                strategyTagMapper.insert(tag);
                //维护标签与明细之间的关系
                strategyDetailMapper.insertStRelationTag(strategyDetail.getId(),tag.getId());
            }

            //明细的文章内容
            strategyContentMapper.update(strategy.getId(),content);

            strategyDetailMapper.updateByPrimaryKey(strategyDetail);
        }else{
            //新增
            strategyDetail.setCreateTime(new Date());
            strategyDetail.setAuthor(null);
            strategyDetailMapper.insert(strategyDetail);
            //创建标签对象
            //遍历添加标签
            StrategyTag tag = new StrategyTag();
            for (String t : tags) {
                tag.setName(t);
                int insert = strategyTagMapper.insert(tag);
                //维护标签与明细之间的关系
                System.out.println(insert+"----------in--------------");
                System.out.println(tag.getId()+"-------get");
                strategyDetailMapper.insertStRelationTag(strategyDetail.getId(),tag.getId());
            }

            //将文章内容保存到strategyContent里

            StrategyContent strategyContent = new StrategyContent();
            strategyContent.setId(strategyDetail.getId());
            strategyContent.setContent(content);
            strategyContentMapper.insert(strategyContent);
        }
    }

    public boolean isChina(Destination dest) {
        if(dest == null){
            return true;
        }
        if("中国".equals(dest.getName())){
            return true;
        }
        if(dest.getParent() != null){
            if("中国".equals(dest.getParent().getName())){
                return true;
            }
        }
        return isChina(dest.getParent());

    }

    public void delete(Long id) {
        strategyDetailMapper.deleteByPrimaryKey(id);
    }

    public StrategyDetail get(Long id) {

        return strategyDetailMapper.selectByPrimaryKey(id);
    }

    public void changeState(Integer state, Long id) {
        strategyDetailMapper.updateState(state,id);
    }

    public List<StrategyDetail> listAll(Long id) {

        return strategyDetailMapper.selectAll(id);
    }

    public StrategyContent getContent(Long id) {
        return strategyContentMapper.selectContent(id);
    }

    public List<StrategyDetail> getReadTop3(Long destId) {

        return strategyDetailMapper.selectAll(destId);
    }

    public List<StrategyDetail> listAll() {
        return strategyDetailMapper.selectList();
    }

    public void updateStatisData(StrategyStatisVO statisVo) {
        strategyDetailMapper.updateStatis(statisVo);
    }

    public List<StrategyDetail> getDetailTop5() {
        return strategyDetailMapper.selectDetailTop5();
    }
}
